package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/role")
//@PreAuthorize("hasRole('ROLE_ADMIN')")
public class RoleController {

    @Reference
    private RoleService roleService;

    //findPage

    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Role> result =null;
        try {
            PageInfo<Role> menuList = roleService.findPage(queryPageBean);
            result = new PageResult<>(menuList.getTotal(),menuList.getList());
            System.out.println(result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取角色失败");
        }
        return new Result(true, "获取角色成功",result);
    }
//findAll
        @PostMapping("/findAll")
        public Result findAll(){
            Map<String,Object> map = new HashMap<>();
            List<Permission> permissionList = roleService.findPermission();
            map.put("permissions",permissionList);
            List<Menu> menuList = roleService.findMenu();
            map.put("menus",menuList);
            return new Result(true, "获取角色信息成功",map);
        }

        //add

    /**
     * 添加角色以及相关表信息
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Role role
            , @RequestParam("permissionIds")Integer[] permissionIds
            ,@RequestParam("menuIds") Integer[] menuIds){

        if (role!=null){
            roleService.addRole(role,permissionIds,menuIds);
        }
        return new Result(true, "添加角色信息成功");
    }

    //findRoleById


    @GetMapping("/findRoleById")
    public Result findRoleById(Integer roleId){
        Map<String,Object> map = new HashMap<>();

        Role role = roleService.findRoleByIdBackRole(roleId);
        List<Integer> permissionIds = roleService.findRolePermissionByRoleId(roleId);
        List<Integer> menuIds = roleService.findRoleMenuByRoleId(roleId);

        map.put("role",role);
        map.put("permissionIds",permissionIds);
        map.put("menuIds",menuIds);
        return new Result(true, "添加角色信息成功",map);
    }


    //edit
    @PostMapping("/edit")
    public Result edit(@RequestBody Role role
            , @RequestParam("permissionIds")Integer[] permissionIds
            ,@RequestParam("menuIds") Integer[] menuIds){

        roleService.updateByRole(role);
        Integer roleId = role.getId();
        roleService.updateBypermissionIds(roleId,permissionIds);
        roleService.updateBymenuIds(roleId,menuIds);

        return new Result(true, "添加角色信息成功",roleId);
    }


    //deleteById
    @PostMapping("/deleteById")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result deleteById(Integer id){
        roleService.deleteById(id);
        return new Result(true, "删除角色信息成功");
    }

    @GetMapping("/findkeywords")
    public Result findkeywords() {
        List<String> rolestrList = roleService.findkeywords();
        return new Result(true, "查询权限关键字成功",rolestrList);
    }
}
